package com.tjetc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.common.Result;
import com.tjetc.entity.Tribe;
import com.tjetc.entity.TribeMessage;
import com.tjetc.entity.User;
import com.tjetc.mapper.TribeMapper;
import com.tjetc.mapper.TribeMessageMapper;
import com.tjetc.service.ITribeMessageService;
import com.tjetc.service.ITribeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjetc.utils.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class TribeServiceImpl extends ServiceImpl<TribeMapper, Tribe> implements ITribeService {
    @Resource
    private TribeMapper tribeMapper;
    @Resource
    private ITribeMessageService tribeMessageService;
    @Autowired
    private MinioUtils minioUtils;


    @Override
    public PageInfo<Tribe> lists(Integer curPage, Integer pageSize, String name) {
        PageHelper.startPage(curPage,pageSize);
        List<Tribe> list=tribeMapper.list(name);
        return new PageInfo<>(list);
    }

    @Override
    public boolean add(Tribe tribe, MultipartFile photo) {
        if (photo!=null){
            String path = minioUtils.upload(photo);
            tribe.setPhotopath(path);
        }
        int insert = tribeMapper.insert(tribe);
        if (insert==1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean updates(Tribe tribe, MultipartFile photo, String[] paths) {
        if (photo!=null){
            String path = minioUtils.upload(photo);
            tribe.setPhotopath(path);
        }
        int i = tribeMapper.updateById(tribe);
        if (i==1){
            return true;
        }else{
            return false;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Tribe> list2(Integer uid) {
        QueryWrapper<TribeMessage> qw=new QueryWrapper<>();
        qw.eq("user_id",uid);
        qw.eq("state",1);
        List<TribeMessage> list= tribeMessageService.list(qw);
        ArrayList<Tribe> list1 = new ArrayList<>();

        for (TribeMessage tribeMessage : list) {
           Tribe tribe=tribeMapper.selectById(tribeMessage.getTribeId());
           list1.add(tribe);
        }

        return list1;
    }


}
